package main

import (
	"fmt"
	"github.com/go-kratos/kratos/v2/config"
	"github.com/go-kratos/kratos/v2/config/file"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/google/uuid"
	"go_private_im/internal/job/conf"
	"go_private_im/internal/job/service"
	"go_private_im/pkg/conversion"
	"go_private_im/third_party/zaplogger"
	"os"
	"os/signal"
	"syscall"
)

var (

	id = uuid.New().String()
)
const (
	version = "2.0.0"
	appid   = "private.comet.job"
)
func main()  {
	c := config.New(config.WithSource(file.NewSource("configs/job.yaml")), config.WithResolver(conversion.YamlTimeToInt))

	if err := c.Load(); err != nil {
		panic(err)
	}
	// 加载配置源：
	v := conf.Config{}
	if err := c.Scan(&v); err != nil{
		panic(err)
	}

	zapLogger := log.With(zaplogger.NewZapLogger(v.Log.Path),
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
		"service.id", id,
		"service.name", appid,
		"service.version", 1,
		"trace_id", tracing.TraceID(),
		"span_id", tracing.SpanID(),
	)

	app :=  service.NewJobService(&v,zapLogger)
	app.Run()
	fmt.Println(app)
	logger := log.NewHelper(zapLogger)
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		s := <-ch
		logger.Infof("goim-job get a signal %s", s.String())
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			app.Close()
			logger.Infof("goim-job [version: %s] exit", version)
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}
